package com.xj.api.bussiness.subscribe.web;

import java.util.List;


import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xj.api.base.web.BaseController;
import com.xj.api.bussiness.subscribe.service.ITbSubscribeService;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.bussiness.subscribe.entity.TbSubscribe;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author CF
 * @since 2019-09-11
 */
@RestController
@RequestMapping("/subscribe")
public class TbSubscribeController extends BaseController {

	@Autowired
	private ITbSubscribeService subscribeService;
	
	@PostMapping("/save")
	@ApiOperation(value = "保存预约信息",  notes = "新增预约")
	public AbstractBean saveSubscribe(@RequestBody TbSubscribe ss){
		if(StringUtils.isNotBlank(ss.getTitle())&&StringUtils.isNotBlank(ss.getTelephone())) {
			TbSubscribe ss1=new TbSubscribe();
			ss1.setTitle(ss.getTitle());
			ss1.setTelephone(ss.getTelephone());
			List<TbSubscribe> selectList = subscribeService.selectList(new EntityWrapper<>(ss1));
			if(CollectionUtils.isNotEmpty(selectList)) {
				for (TbSubscribe tbSubscribe : selectList) {
					if(tbSubscribe.getState()==null||tbSubscribe.getState()==3) {
						return fail("您已经通过此手机号预约过了", "-1");
					}
				}
			}
//			ss.setProvince(TbSubscribe.getAdrName(ss.getProvince()));
//			ss.setCity(TbSubscribe.getAdrName(ss.getCity()));
//			ss.setCounty(TbSubscribe.getAdrName(ss.getCounty()));
			ss.setState(3);
			boolean insert = subscribeService.insert(ss);
			if(insert) {
				return success();
			}
		}
		
		
		return error();
	}
	@PostMapping("/get/{title}/{telephone}")
	@ApiOperation(value = "保存预约信息",  notes = "新增预约")
	public AbstractBean getSubscribe(@PathVariable String title,@PathVariable String telephone){
		TbSubscribe ss=new TbSubscribe();
		ss.setTitle(title);
		ss.setTelephone(telephone);
		ss.setState(3);
		return json(subscribeService.selectOne(new EntityWrapper<>(ss)));
		
	}
	@GetMapping("/getRegion")
	@ApiOperation(value = "获取中国所有地址",  notes = "获取地址")
	public AbstractBean getRegion(){
		return json(TbSubscribe.getAdr(0));
		
	}
	
}
